/* GM_ replacement functions */  

if (typeof(GM_getValue) == 'undefined')
{
	// GM_get/setValue based on pup's cookie code again
	GM_getValue = function(name, defaultValue) 
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) 
		{
			begin = dc.indexOf(prefix);
			if (begin != 0) return defaultValue;
		}
		else
		{
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
		{
			end = dc.length;
		}
		return unescape(dc.substring(begin + prefix.length, end));
	};
}

if (typeof(GM_setValue) == 'undefined')
{
	GM_setValue = function (name, value, future) 
	{
		if (future == null) 
		{
			future = 5;
		}
		var tempdate = new Date();
		tempdate = tempdate.getTime() + ((future - 1 + future) * (7 * 24 * 60 * 60 * 1000));
		var cookieDate = new Date(tempdate);
		var curCookie = name + "=" + escape(value) + "; expires=" + cookieDate.toGMTString() + "; path=/; domain=shacknews.com";
		document.cookie = curCookie;
	};
}

// GM_addStyle should no longer be used by any of the scripts 
if (typeof(GM_addStyle) == 'undefined')
{
	// thx http://meyerweb.com/eric/thoughts/2004/11/10/s5-11a2/
	GM_addStyle = function (string)
	{
		// GM_log('DO NOT USE GM_ADDSTYLE IT IS RIDICULOUSLY SLOW'); 
	
		if(/microsoft/i.test(navigator.appName) && !/opera/i.test(navigator.userAgent))
		{
			document.createStyleSheet().cssText=string;
		}
		else 
		{
			var ele=document.createElement('link');
			ele.rel='stylesheet';
			ele.type='text/css';
			ele.href='data:text/css;charset=utf-8,'+escape(string);
			document.getElementsByTagName('head')[0].appendChild(ele);
		}
	};
}

if (typeof(GM_log) == 'undefined')
{
	GM_log = function(str) 
	{ 
		console.log(str); 
	}; 
}
/* END GM REPLACEMENT FUNCTIONS */


function parseQuerystring(qs)
{
	var ret = new Array(); 
	var q = qs.split('?');
	if (q.length == 2)
	{
		// Grab querystring part of url and convert plusses to spaces  
		q = q[1].replace('+', ' '); 
		
		// kp = keypairs
		kp = q.split('&'); 
		
		for (var i = 0; i< kp.length; i++)
		{
			var p = kp[i].split('=');
			if (p.length == 2)
			{
				ret[p[0]] = decodeURIComponent(p[1]); 
			}
			else
			{
				ret[p[0]] = null;
			}
		}
	} 
	return ret; 
}


/* OPTIONS */

function shackExtScript(name, description, enabledDefault) 
{
    this.name = name;
    this.description = description;
    this.enabledDefault = enabledDefault;
}

shackExtScripts =
{
	'collapse_post': new shackExtScript('Collapse Fixer', 'Makes the collapse buttons actually work! (Persistent collapsing).', "true"),
	'Lolscript': new shackExtScript('Shack[lol]', 'Adds [lol] link to posts. You want this.', "true"),
	'comment_tags': new shackExtScript('Comment Tags', 'Adds &quot;Shack Comment Tags&quot; Legend besides the comment form. Try out the tag hotkeys with Alt Shift in Windows, Alt on Macs.', "true"),
	'post_preview': new shackExtScript('Post Preview', 'Adds a Preview button next to Post. Preview your posts before posting.', "true"),
	'embed_video': new shackExtScript('Video Embedinator', 'Adds embedded videos to the comments.', "false"),
	'message_user': new shackExtScript('Message This User', 'Adds Message This User buttons back to the comments.', "true"),
	'shack_message_notify': new shackExtScript('Shackmessage Notifier', 'Turns your message count green when you have a new message', "true"),
	'category_banners': new shackExtScript('Category Banners', 'Adds &quot;NWS&quot; style banners on top of other type of posts. (Stupid, Political, etc.)', "true"),
	'new_comments_marker': new shackExtScript('New Comments Marker', 'Marks which comments are new since your last refresh.', "false"),
	'mod_marker' : new shackExtScript('Mod Marker', 'Mark mods in green', "false"), 
	'shacktimezone' : new shackExtScript('Shack Timezone', 'Modify all timestamps to local times', "false"), 
	'gegtiks_dinosaur_comics' : new shackExtScript('Gegtik\s Dinosaur Comics', 'Turns all of gegtik\'s posts into Dinosaur Comics!', "false"),
	'sparkly_comics' : new shackExtScript('Sparkly Comics', 'Turns all of sparkly\'s posts into hilarious Sparkly Comics!', "false"),
	'work_shack': new shackExtScript('Websense.Shack', 'Changes all your links so shack doesn\'t get blocked by work proxies', "false"), 
	'shugashack': new shackExtScript('Shugashack!', 'Welcome to 1999! Happy 10th Birthday to my Shack account!', "false"),
	'friendfoe': new shackExtScript('Friend/Foe', 'Colorize the names of Shackers you\'d like to keep tabs on, plus other neat stuff.  Click on the icon in the upper left in your account settings page for additional configuration.', "false")
	// 'navigation_keys': new shackExtScript('Navigation Keys', 'Press &quot;?&quot; for a list of shortcuts available after install. Useful keys: H, J, K, L, v.', "true"),
	// 'confirm_unload': new shackExtScript('Confirm Unload', 'Prompts the user to confirm they want to navigate away from the shack before doing it in an attempt to track down that nasty a/v ad.', 'false')  
	// 'image_loader' : new shackExtScript('Image Loader', 'Loads an image in the post when an image link is clicked. Click the image to hide. Click on the right side of the image to enlarge', "true"),
}


// Make sure each of the scripts have their default values saved to cookies so 
// there aren't any undefined values so main.js can inject only the scripts that 
// are actually activated by the user    
for(var index in shackExtScripts) 
{
	if (GM_getValue(index, 'unset') == 'unset')
	{
		GM_setValue(index, shackExtScripts[index].enabledDefault); 
	}
}
	
toggle_script = function(script_key, enabled) 
{
    GM_setValue(script_key, enabled);
}

// if the script is in the off by default array, then the user has to turn it on
script_enabled = function(script_key) 
{
    return GM_getValue(script_key, shackExtScripts[script_key].enabledDefault);
}

if (document.getElementById('commentssettings') != null) 
{
	script_options = document.createElement('div');
	script_options.setAttribute("id", "shackscripts_options");
	script_options.style.clear = 'both'; 
	script_options_html = "<div class=\"interiorform\"><h5>Shackscripts Extension Settings</h5>";
	
	var i = 0; 
	
	for (key in shackExtScripts) 
	{
		var classPosition = (i % 2 == 0) ? 'first' : 'last';
		i++;
		var isChecked = (script_enabled(key) == 'true') ? ' checked="checked"' : '';
		script_options_html += '<div class="control condense ' + classPosition + '"><label for="toggle_' + key + '" title="' + shackExtScripts[key].description + '"><input type="checkbox" ' + isChecked + '" id="toggle_' + key + '" onclick="toggle_script(\'' + key + '\',this.checked);" />' + shackExtScripts[key].name + '</label></div>';
	}
	
	script_options_html += '</div>'; // /interiorform
	
	script_options_html += '<div id="shackscripts_links" style="clear: both;"><ul><li><a href="http://code.google.com/p/shackscripts/wiki/Main">Shackscripts Extension Home</a></li><li><a href="http://code.google.com/p/shackscripts/">Shackscripts Extension Google Code Project</a></li></ul></div>';
	
	script_options.innerHTML = script_options_html;
	
	document.getElementById('commentssettings').appendChild(script_options);
}
	
/* END OPTIONS */
